package com.jrx.anytxn.param.controller.partner;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.partner.PmPartnerInfoRep;
import com.jrx.anytxn.param.dto.partner.PmPartnerInfoRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.partner.IPmPartnerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * 合作伙伴配置表
 *
 * @author lbz
 */
@Api(tags = "合作伙伴配置API接口")
@RestController
@RequestMapping(value = "/param/partner")
public class PartnerParamController {

    private static final Logger logger = LoggerFactory.getLogger(PartnerParamController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPmPartnerInfoService pmPartnerInfoService;

    @ApiOperation(value = "根据租户ID和合作伙伴ID查询", notes = "根据租户ID和合作伙伴ID查询")
    @GetMapping(value = "/{tenantId}/{partnerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "partnerId", value = "合作伙伴ID", required = true)
    })
    public TxnRespResult<PmPartnerInfoRes> getById(@NotNull @PathVariable String tenantId, @PathVariable String partnerId) {
        TxnRespResult<PmPartnerInfoRes> result = new TxnRespResult<>();
        PmPartnerInfoRes res;
        try {
            res = pmPartnerInfoService.getByTenIdIdAndPartnerId(String.valueOf(infoService.getUser().getProjectId()), partnerId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增合作伙伴信息", notes = "新增合作伙伴信息")
    @PostMapping("/")
    public TxnRespResult<PmPartnerInfoRes> createInterestTable(@RequestBody PmPartnerInfoRep pmPartnerInfoRep) {
        TxnRespResult<PmPartnerInfoRes> result = new TxnRespResult<>();
        PmPartnerInfoRes res;
        try{
            pmPartnerInfoRep.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            pmPartnerInfoRep.setUpdateBy(infoService.getUser().getUserName());
            pmPartnerInfoRep.setIdType("BO1");
            res = pmPartnerInfoService.addPartnerInfo(pmPartnerInfoRep);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询合作伙伴参数信息", notes = "分页查询合作伙伴参数信息")
            @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
            @ApiImplicitParams({
                    @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
            public TxnRespResult<TxnPage<PmPartnerInfoRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
                TxnRespResult<TxnPage<PmPartnerInfoRes>> result = new TxnRespResult<>();
                TxnPage<PmPartnerInfoRes> response;
                try {
                    String tenantId=String.valueOf(infoService.getUser().getProjectId());
                    response = pmPartnerInfoService.findAllPageByTenantId(pageNum, pageSize, tenantId);
                    result.getSuccess(response);
                } catch (TxnException e) {
                    logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑短信模板参数表信息", notes = "根据租户ID编辑短信模板参数表信息")
    @PutMapping("/")
    public TxnRespResult<PmPartnerInfoRes> modifyPmPartnerInfo(@RequestBody PmPartnerInfoRep request) {
        TxnRespResult<PmPartnerInfoRes> result = new TxnRespResult<>();
        PmPartnerInfoRes res;
        try {
            request.setUpdateBy(infoService.getUser().getUserName());
            res = pmPartnerInfoService.modifyPmPartnerInfo(request);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和合作伙伴ID删除信息", notes = "根据租户ID和合作伙伴ID删除信息")
    @DeleteMapping(value = "/{tenantId}/{partnerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "partnerId", value = "合作伙伴ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndPartnerId(@NotNull @PathVariable String tenantId, @PathVariable String partnerId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = pmPartnerInfoService.removeByTenantIdAndPartnerId(String.valueOf(infoService.getUser().getProjectId()), partnerId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


    @ApiOperation(value = "获得合作伙伴下拉信息", notes = "获得合作伙伴下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PmPartnerInfoRes>>> getPartnerInfoList() {
        TxnRespResult<HashMap<String, List<PmPartnerInfoRes>>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PmPartnerInfoRes> allPartner = pmPartnerInfoService.findAllByTenId(tenantId);
        HashMap<String, List<PmPartnerInfoRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allPartner);
        return result.getSuccess(sysCodeMap);
    }

}
